# 保存脚本初始目录，以便后面跳转
current_dir=$(pwd)
# 操作系统类型
os_type=$(uname)
# 配置公钥的帮助文档地址
helpUrl=https://www.cnblogs.com/zhuawang/p/12433357.html
# 服务器ip
serverHost=101.37.69.211
# 后台代码部署路径
smtPath=/opt/blb-smt
# staff代码部署路径
staffPath=/usr/share/nginx/html/staff
# student代码部署路径
studentPath=/usr/share/nginx/html/student

### 检查JDK环境

# 是否配置
echo "是否已经将本地公钥配置到服务器（用于免密连接服务器）"
echo "1.是"
echo "2.否"
read c
if [ $c -eq 2 ]; then
	if [ $os_type == Darwin ]; then
		open $helpUrl
	elif [ $os_type == Linux ]; then
		xdg-open $helpUrl
	else 
		start $helpUrl
	
	echo 已打开相关文档，请按照文档进行配置公钥即可使用该脚本
	exit 1	
	fi
fi


# JDK版本
echo 正在检查系统环境
JDK_VERSION=$(java -version 2>&1 | awk -F[\"_] 'NR==1 {print $2}')
MAJOR_VERSION=$(echo $JDK_VERSION | cut -d. -f1)
MINOR_VERSION=$(echo $JDK_VERSION | cut -d. -f2)
if [ "$MAJOR_VERSION" -lt 17 ] || [ "$MAJOR_VERSION" -eq 17 -a "$MINOR_VERSION" -lt 0 ]; then
    echo "系统环境不满足要求，需要JDK17及以上版本。当前版本：$JDK_VERSION"
    exit 1
else
    echo "系统环境满足要求，JDK版本：$JDK_VERSION"
fi


### 拉取代码
echo 
echo 正在拉取最新代码...
git pull
echo 拉取代码完毕！

### 菜单
echo 
while true; do
  echo "1.重新部署全部"
  echo "2.重新部署blb-smt"
  echo "3.重新部署blb-smt-staff-ui"
  echo "4.重新部署blb-smt-student-ui"
  echo "5.重新部署staff和student服务"
  echo "6.退出"
  echo "请选择："
  read n

  if [ $n -ge 1 ] && [ $n -le 6 ]; then
    break
  else
    echo "输入错误，请重新选择。"
  fi
done

### 重新部署blb-smt
updateBs(){
	echo 
	echo -------------------------blb-smt-------------------------------
	echo 开始重新部署blb-smt...
	cd $current_dir/03代码/blb-smt
	echo 开始打包blb-smt...
	mvn clean package
	echo 打包完毕!
	echo 
	echo 正在停止blb-smt服务...
	ssh root@$serverHost systemctl stop blb-smt
	echo 正在删除原来的文件
	ssh root@$serverHost rm -rf $smtPath/*
	echo 准备上传blb-smt代码至服务器
	scp target/blb-smt-0.0.1-SNAPSHOT.jar root@$serverHost:$smtPath
	echo 上传完毕!	
	echo 正在启动blb-smt服务...
	ssh root@$serverHost systemctl start blb-smt	
	ssh root@$serverHost systemctl status blb-smt | grep "active (running)"
	if [ $? -eq 0 ]; then
	  echo "blb-smt启动成功!"
	else
	  echo "blb-smt启动失败，请检查原因"
	  exit 1
	fi
	echo blb-smt重新部署完毕!!!   
	echo -------------------------blb-smt-------------------------------
}

### 重新部署blb-smt-staff-ui
updateBsf(){
	echo 
	echo -------------------------blb-smt-staff-ui-------------------------------
	echo 开始重新部署blb-smt-staff-ui...
	cd $current_dir/03代码/blb-smt-staff-ui
	echo 开始下载相关依赖
	npm i
	echo 开始打包blb-smt-staff-ui...	
	npm run build
	echo 打包完毕!
	echo 
	echo 正在停止blb-smt-staff-ui服务...
	ssh root@$serverHost systemctl stop nginx
	echo 正在删除原来的文件
	ssh root@$serverHost rm -rf $staffPath/*
	echo 准备上传blb-smt-staff-ui代码至服务器
	scp -r staff/* root@$serverHost:$staffPath	
	echo 上传完毕!	
	echo 正在启动blb-smt-staff-ui服务...
	ssh root@$serverHost systemctl start nginx
	ssh root@$serverHost systemctl status nginx | grep "active (running)"
	if [ $? -eq 0 ]; then
	  echo "blb-smt-staff-ui启动成功!"
	else
	  echo "blb-smt-staff-ui启动失败，请检查原因"
	  exit 1
	fi
	echo blb-smt-staff-ui重新部署完毕!!!   
	echo -------------------------blb-smt-staff-ui-------------------------------
}

### 重新部署blb-smt-student-ui
updateBss(){
	echo 
	echo -------------------------blb-smt-student-ui-------------------------------
	echo 开始重新部署blb-smt-student-ui...
	cd $current_dir/03代码/blb-smt-student-ui
	echo 开始下载相关依赖
	npm i
	echo 开始打包blb-smt-student-ui...
	npm run build
	echo 打包完毕!
	echo 
	echo 正在停止blb-smt-student-ui服务...
	ssh root@$serverHost systemctl stop nginx
	echo 正在删除原来的文件
	ssh root@$serverHost rm -rf $studentPath/*
	echo 准备上传blb-smt-student-ui代码至服务器
	scp -r student/* root@$serverHost:$studentPath
	echo 上传完毕!	
	echo 正在重启blb-smt-student-ui服务...
	ssh root@$serverHost systemctl start nginx
	ssh root@$serverHost systemctl status nginx | grep "active (running)"
	if [ $? -eq 0 ]; then
	  echo "blb-smt-student-ui启动成功!"
	else
	  echo "blb-smt-student-ui启动失败，请检查原因"
	  exit 1
	fi
	echo blb-smt-student-ui重新部署完毕!!!   
	echo -------------------------blb-smt-student-ui-------------------------------
}

### 重新部署全部
updateAll(){
	echo 
	updateBs
	updateBsf
	updateBss
	echo 全部代码部署完毕
	echo -------------------------部署全部-------------------------------
}


if [ $n -eq 1 ]; then
	updateAll
elif [ $n -eq 2 ]; then
	updateBs
elif [ $n -eq 3 ]; then
	updateBsf
elif [ $n -eq 4 ]; then
	updateBss
elif [ $n -eq 5 ]; then
	updateBsf
	updateBss	
else 
	exit 1
fi